//将页面上的数据封装成house对象并返回
function getHouse() {
    let house = new Object();
    house.houseName = $("[name='house_name']").val();
    house.houseRentalType = $("[name='house_rental_type']").val();
    house.houseType = $("[name='house_type']").val();
    house.houseRegion = $("[name='house_region']").val();
    let housePrice = $("[name='house_price']").val();
    let s = housePrice.split("-");
    house.houseMinPrice = s[0];
    house.houseMaxPrice = s[1];
    house.houseArea = $("[name='house_area']").val();
    house.houseRent = $("[name='house_rent']").val();
    house.houseImage = $("[name='house_image']").attr("src");
    house.houseIntroduction = $("[name='house_introduction']").val();
    return house;
}

//获取url上的参数
function getQueryString(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r!=null) return r[2]; return '';
}

//将house对象中的值在detail页面展示
function showDetail(req) {
    if(req.code == 200) {
        let data = req.data;
        console.log(data);
        $("[name='house_name']").val(data.houseName);
        $("[name='house_rental_type']").val(data.houseRentalType);
        $("[name='house_type']").val(data.houseType);
        $("[name='house_region']").val(data.houseRegion);
        let housePrice = data.houseMinPrice + "-" + data.houseMaxPrice;
        $("[name='house_price']").val(housePrice);
        $("[name='house_area']").val(data.houseArea);
        $("[name='house_rent']").val(data.houseRent);
        $("[name='house_image']").attr("src", data.houseImage);
        $("[name='house_introduction']").val(data.houseIntroduction);
    }
}

//展示my-house界面
function showMyHouse(req) {
    if(req.code == 200) {
        $('.table-my-house > tbody').html('');
        console.log("查询成功");
        console.log(req.data);
        let data = req.data;
        let str = "";//定义用于拼接的字符串
        for (let i = 0; i < data.length; i++) {
            //出租方式
            let houseRentalType = "";
            //出租状态
            let houseStatus = "";
            if (data[i].houseRentalType == "0") {
                houseRentalType = "短租"
            } else {
                houseRentalType = "长租";
            }
            if (data[i].houseStatus == "0") {
                houseStatus = "未出租"
            } else {
                houseStatus = "已出租";
            }
            //拼接表格的行和列
            str = "<tr value = " + data[i].houseId + ">" +
                "<td>" + data[i].houseName + "</td>" +
                "<td>" + houseRentalType + "</td>" +
                "<td>" + data[i].houseRegion + "</td>" +
                "<td>" + data[i].houseRent + "</td>" +
                "<td>" + houseStatus + "</td>" +
                "<td>" + data[i].houseIntroduction + "</td>" +
                "<td>" +
                "<button type='button' class='btn btn-success btn-sm house-detail'>详情</button>" +
                "<button type='button' class='btn btn-danger btn-sm house-deleted'>删除</button>" +
                "</td>" +
                "</tr>";
            //追加到table中
            $(".table-my-house > tbody").append(str);
        }
    }
}

function showMyOrder(req) {
    if(req.code == 200) {
        $('.table-my-order > tbody').html('');
        console.log("查询成功");
        console.log(req.data);
        let data = req.data;
        let str = "";//定义用于拼接的字符串
        for (let i = 0; i < data.length; i++) {
            //拼接表格的行和列
            str = "<tr value = " + data[i].orderId + ">" +
                "<td>" + data[i].houseName + "</td>" +
                "<td>" + data[i].tenantName + "</td>" +
                "<td>" + data[i].orderCheckInTime + "</td>" +
                "<td>" + data[i].orderLeaveTime + "</td>" +
                "<td>" + data[i].orderTime + "</td>" +
                "<td>" + data[i].orderPrice + "</td>" +
                "<td>" +
                // "<button type='button' class='btn btn-success btn-sm order-detail'>详情</button>" +
                "<button type='button' class='btn btn-danger btn-sm order-deleted'>删除</button>" +
                "</td>" +
                "</tr>";
            //追加到table中
            $(".table-my-order > tbody").append(str);
        }
    }
}

function showMyEvaluation(req) {
    if(req.code == 200) {
        $('.table-my-evaluation > tbody').html('');
        console.log("查询成功");
        console.log(req.data);
        let data = req.data;
        let str = "";//定义用于拼接的字符串
        for (let i = 0; i < data.length; i++) {
            //拼接表格的行和列
            str = "<tr value = " + data[i].evaluateId + ">" +
                "<td>" + data[i].houseName + "</td>" +
                "<td>" + data[i].tenantName + "</td>" +
                "<td>" + data[i].evaluateTime + "</td>" +
                "<td>" + data[i].evaluateOwner + "颗星</td>" +
                "<td>" + data[i].evaluateHouse + "颗星</td>" +
                "<td>" + data[i].evaluateContent + "</td>" +
                "<td>" +
                // "<button type='button' class='btn btn-success btn-sm order-detail'>详情</button>" +
                "<button type='button' class='btn btn-danger btn-sm evaluation-deleted'>删除</button>" +
                "</td>" +
                "</tr>";
            //追加到table中
            $(".table-my-evaluation > tbody").append(str);
        }
    }
}

function showRentOutMessage(req) {
    if(req.code == 200) {
        $('.table-rent-out-message > tbody').html('');
        console.log("查询成功");
        console.log(req.data);
        let data = req.data;
        let str = "";//定义用于拼接的字符串
        for (let i = 0; i < data.length; i++) {
            //拼接表格的行和列
            str = "<tr value = " + data[i].orderId + ">" +
                "<td>" + data[i].houseName + "</td>" +
                "<td>" + data[i].tenantName + "</td>" +
                "<td>" + data[i].orderCheckInTime + "</td>" +
                "<td>" + data[i].orderLeaveTime + "</td>" +
                "<td>" + data[i].orderTime + "</td>" +
                "<td>" + data[i].orderPrice + "</td>" +
                "<td>" +
                "<button type='button' class='btn btn-danger btn-sm rent-out-confirm'>出租</button>" +
                "</td>" +
                "</tr>";
            //追加到table中
            $(".table-rent-out-message > tbody").append(str);
        }
    }
}